#!/usr/bin/env python

import MySQLdb

def connect(ip) :
	conn = MySQLdb.connect(ip, 'gamevil', 'vlwlrpaqlf', 'pz_user')
	csr  = conn.cursor()
	return (conn, csr)

connMap = {}
connMap["100"] = connect('172.16.1.183')
connMap["101"] = connect('172.16.2.40')
connMap["102"] = connect('172.16.2.36')
connMap["103"] = connect('172.16.2.37')
connMap["104"] = connect('172.16.2.38')
connMap["105"] = connect('172.16.2.39')

sql = """
	SELECT status FROM info_item
	WHERE user_seq = %d
	  AND reg_date >= 1391958000
	  AND reg_date <= 1391966440
	  AND quality >= 2
	  AND bag_type IN (0, 1)
  """

fd = open("userinfo.txt")
for line in fd.xreadlines() :
	user_seq, shard_type, dummy = line.strip().split(",", 2)
	conn, csr = connMap.get(shard_type)
	csr.execute(sql % int(user_seq))

	normal_item, delete_item = (0, 0)

	for res in csr.fetchall() :
		item_status = int(res[0])
		if (item_status == 0) : normal_item += 1
		elif (item_status == 3) : delete_item += 1
	
	print "%d,%d,%d" % (int(user_seq), normal_item, delete_item)
fd.close()


for shard_type in connMap.keys() :
	print "close... %s" % shard_type
	conn, csr = connMap.get(shard_type)
	csr.close()
	conn.close()
